package com.project.blog.config;


import com.project.blog.auth.OAuthRealm;
import com.project.blog.auth.OAuthSessionManager;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.session.mgt.eis.MemorySessionDAO;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    /**
     * shrio过滤器，添加过滤那些权限和不过滤那些权限
     * @param sm
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager sm) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

        shiroFilterFactoryBean.setSecurityManager(sm);
        // # 用LinkedHashMap添加拦截的uri,其中authc指定需要认证的uri，anon指定排除认证的uri
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        //anon指定排除认证的uri
        filterChainDefinitionMap.put("/**", "anon");
        //返回json数据，由前端跳转 设置登录失败，授权成功、
        shiroFilterFactoryBean.setLoginUrl("/handleLogin");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }

    /**
     * 进行的是加盐密文密码的比对
     * 我们在创建HashedCredentialsMatcher时，
     * 要给其设置hash算法和hash的次数（和加密密码时使用的hash算法和hash次数要一致）。
     * @return
     */
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //加密方法
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        //hash的次数
        hashedCredentialsMatcher.setHashIterations(2);
        return hashedCredentialsMatcher;
    }

    /**
     * 注入自己获取用户信息的realm
     * @return
     */
    @Bean
    public OAuthRealm oAuthRealm() {
        OAuthRealm myShiroRealm = new OAuthRealm();
        myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher());
        return myShiroRealm;
    }

    /**
     * shiro 委托securityManager 进行身份验证
     * @param oAuthRealm
     * @param sessionManager
     * @return
     */
    @Bean
    public SecurityManager securityManager(OAuthRealm oAuthRealm, SessionManager sessionManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(oAuthRealm);
        securityManager.setSessionManager(sessionManager);
        // 自定义缓存实现 使用redis
        //securityManager.setCacheManager(cacheManager());
        return securityManager;
    }


    @Bean
    public SessionManager sessionManager() {
        OAuthSessionManager oAuthSessionManager = new OAuthSessionManager();
        //使用默认的内存管理
        oAuthSessionManager.setSessionDAO(new MemorySessionDAO());
        return oAuthSessionManager;
    }



    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }
}
